package com.smartrobot.smartrobot.pagerHolder;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.smartrobot.smartrobot.Common.Common;
import com.smartrobot.smartrobot.Common.CommonFunc;
import com.smartrobot.smartrobot.R;
import com.smartrobot.smartrobot.adapter.MainMessageAdapter;
import com.smartrobot.smartrobot.logToSdCard.LogToSd;
import com.smartrobot.smartrobot.util.FileUtil;
import com.smartrobot.smartrobot.util.Message;
import com.smartrobot.smartrobot.util.User;

import java.util.ArrayList;
import java.util.List;

public class MessagePagerHolder extends Fragment implements BaseFragment{

    private View view;
    private boolean isFirst = true;
    private Context context;
    private TextView progressTxt;

    private ListView list;
    private MainMessageAdapter messageAdapter;

    private List<Message> chatList;
    private Handler readFileHandler;


    public MessagePagerHolder() {

    }

    /**
     * 返回的view为滑动显示的view
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.page_main_message,container,false);

        Log.d("==========>", "fragment 显示  " + "消息");

        return view;
    }

    @Override
    public void onStart() {

//        init();

        super.onStart();
    }

    public void init(){


        if(isFirst){//若是第一次就初始化数据
//            isFirst = false;

            if(getActivity() == null) throw new NullPointerException();
            chatList = new ArrayList<Message>();
            readFileHandler = new Handler();

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    progressTxt = (TextView)view.findViewById(R.id.main_message_progress_text);

                    list = (ListView) view.findViewById(R.id.main_message_listview);
                    messageAdapter = new MainMessageAdapter(getActivity(),chatList);
                    list.setAdapter(messageAdapter);
                    progressTxt.setVisibility(View.GONE);

                    initAdepter();
                    readChatFromLocal();

                }
            }, 500);


        }



    }

    private void initAdepter(){

        messageAdapter = new MainMessageAdapter(getActivity(),chatList);
        list.setAdapter(messageAdapter);
        list.setOnItemClickListener(messageAdapter);
    }


    private void readChatFromLocal(){
        Thread t = new Thread(){
            @Override
            public void run(){
                try{
                    String path =  CommonFunc.stringToMD5(User.getInstance().getId() +
                            Common.chat_file);
                    List<Message>temp = new  ArrayList<Message>();
                    temp =  (List<Message>) FileUtil.ReadCircleMsgFromFile(path, getActivity());
                    if(temp!=null){
                        chatList = temp;
                        readFileHandler.post(updateChatListFromLocal);
                    }
                }catch (Exception e){
                    LogToSd.log_init();
                    LogToSd.error("readChatFromLocal error line58:"+e.toString());
                    Log.e("===========readChatFromLocal==========", e.toString());
                }

            }
        };
        t.start();
    }


    final Runnable updateChatListFromLocal = new Runnable() {

        @Override
        public void run() {
            messageAdapter.notifyChange(chatList);
        }
    };

}
